package cn.hgd11.security.filter;

import cn.hgd11.security.entity.SysUser;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author wangkaige 
 */
@Slf4j
public class SimpleTokenAuthenticationFilter extends OncePerRequestFilter {
    private static final String AUTHORIZATION = "Authorization";
    private static final String BEARER = "Bearer ";

    private StringRedisTemplate stringRedisTemplate;

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        String authToken = request.getHeader(AUTHORIZATION);
        if (!StringUtils.isNotBlank(authToken)) {
            String token = StringUtils.removeStart(authToken, BEARER);
            String user = stringRedisTemplate.opsForValue().get(token);

            if (StringUtils.isNotBlank(user)) {
                SysUser sysUser = JSONObject.parseObject(user, SysUser.class);
                /\/\
                SecurityContext context = SecurityContextHolder.getContext();
            }
        }
    }
}
